package softtech.apps.callblocker;

/**
 * 
 * @author Hoai
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import softtech.apps.utility.ApplicationConstants;
import softtech.apps.callblocker.ActivityChat;
import softtech.apps.callblocker.R;
import softtech.apps.callblocker.adapter.AdapterCallHistory;
import softtech.apps.callblocker.adapter.AdapterMessengerHistory;
import softtech.apps.callblocker.model.CallBlockedItem;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderSMSBlockedItem;
import softtech.apps.callblocker.model.SmsBlockedItem;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;

public class ActivityCallHistory extends Activity {
	final String TAG = "Activity Call History";
	ListView listView;
	ListView lvMessengerHistory;
	String CurrentDateCall = "";
	LayoutInflater inf;
	AdapterCallHistory adapter;
	ViewFlipper vfhistory;
	Button btMessengerHistory;
	Button btCallHistory;
	//
	/**
	 * List Messenger
	 */
	String CurrentDateMessenger = "";
	ArrayList<String> PositionDateMessenger = new ArrayList<String>();
	ArrayList<SmsBlockedItem> MessengerListHistory = new ArrayList<SmsBlockedItem>();
	ArrayList<HeaderSMSBlockedItem> MessengerListHeader = new ArrayList<HeaderSMSBlockedItem>();
	/**
	 * Call list
	 */
	ArrayList<CallBlockedItem> CallListHistory = new ArrayList<CallBlockedItem>();
	ArrayList<HeaderSMSBlockedItem> CallListHeader = new ArrayList<HeaderSMSBlockedItem>();
	AdapterMessengerHistory AdapterSMSHistory;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_call_history);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setTitle("History");
		registerReceiver(broadcastUpdateForm, new IntentFilter(
				ApplicationConstants.TAG_BROADCAST));
		/**
		 * Test
		 */
		// finsertCallBlock(ActivityCallHistory.this);
		// finsertMessengerBlock(ActivityCallHistory.this);
		/**
		 * 
		 * Call History
		 * 
		 */
		inf = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		listView = (ListView) findViewById(R.id.listView1);
		// call get daytime
		getCallDetails(getApplicationContext());
		// display listview
		adapter = new AdapterCallHistory(CallListHeader, inf,
				getApplicationContext());
		listView.setAdapter(adapter);
		listView.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisible,
					int visibleCount, int totalCount) {
				boolean loadMore = // /maybe add a padding
				firstVisible + visibleCount >= totalCount - 10;
				if (loadMore && adapter.count < CallListHeader.size()) {
					adapter.count += 30; // or any other amount
					adapter.notifyDataSetChanged();
				}
			}
		});
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				/**
				 * display info mation
				 */
				if (CallListHeader.size() > 0) {
					fInfoHistory(position);
				}
			}
		});
		// View Flipper
		vfhistory = (ViewFlipper) findViewById(R.id.vfHistory);
		btMessengerHistory = (Button) findViewById(R.id.btMessengerlistHistory);
		btCallHistory = (Button) findViewById(R.id.btCallListHistory);
		// show display messenger list history
		btMessengerHistory.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				fMessengerHistory();

			}
		});
		// Show display call list history
		btCallHistory.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				fCallHistory();
			}
		});

		/**
		 * Messenger History
		 * 
		 */
		lvMessengerHistory = (ListView) findViewById(R.id.lvMessengerHistory);
		// insert to test
		// finsertMessengerBlock(getApplicationContext());
		// get all messenger
		fgetMessengerBlock(getApplicationContext());
		AdapterSMSHistory = new AdapterMessengerHistory(MessengerListHeader,
				inf, getApplicationContext());
		lvMessengerHistory.setAdapter(AdapterSMSHistory);
		/**
		 * Scroll view
		 */
		lvMessengerHistory
				.setOnScrollListener(new AbsListView.OnScrollListener() {

					@Override
					public void onScrollStateChanged(AbsListView view,
							int scrollState) {

					}

					@Override
					public void onScroll(AbsListView view, int firstVisible,
							int visibleCount, int totalCount) {
						boolean loadMore = // /maybe add a padding
						firstVisible + visibleCount >= totalCount - 10;
						Log.i("ok", "Count: " + adapter.count
								+ " totalItemCount  " + totalCount + " "
								+ visibleCount + " " + firstVisible);
						if (loadMore
								&& AdapterSMSHistory.count < MessengerListHeader
										.size()) {
							AdapterSMSHistory.count += 30; // or any other
															// amount
							AdapterSMSHistory.notifyDataSetChanged();
						}

					}
				});
		lvMessengerHistory
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
						if (MessengerListHeader.size() > 0) {
							Intent intent = new Intent(
									ActivityCallHistory.this,
									ActivityChat.class);
							intent.putExtra("phonenumber", MessengerListHeader
									.get(position).getPhoneNumber());
							intent.putExtra("setintent", 0);
							startActivity(intent);
							finish();
						}
					}

				});
		final Dialog dialog = new Dialog(ActivityCallHistory.this);
		fMessengerShowOption(dialog);
		lvMessengerHistory
				.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> arg0,
							View arg1, int arg2, long arg3) {
						dialog.show();
						final int id = arg2;
						final String smsphonenumber = MessengerListHeader.get(
								id).getPhoneNumber();
						final String smsContent = MessengerListHeader.get(id)
								.getContent();
						btoutgoingcall
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										fRecall(id, getApplicationContext());
										dialog.dismiss();
									}
								});
						btSendSMS
								.setOnClickListener(new View.OnClickListener() {

									@Override
									public void onClick(View v) {
										Intent intent = new Intent(
												ActivityCallHistory.this,
												ActivityChat.class);
										intent.putExtra("phonenumber",
												smsphonenumber);
										startActivity(intent);
										dialog.dismiss();
										ActivityCallHistory.this.finish();
									}
								});
						btDelete.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								final DatabaseHanler dbh = new DatabaseHanler(
										getApplicationContext());
								dialog.dismiss();
								AlertDialog.Builder aldialog = new AlertDialog.Builder(
										ActivityCallHistory.this);
								aldialog.setMessage("Do you want to delete messenger of number?");
								aldialog.setCancelable(true);
								aldialog.setPositiveButton("Yes",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												dbh.deleteSmsBlockItem(
														SmsBlockedItem.CONTENT_SMS
																+ " =?",
														new String[] { smsContent });
												MessengerListHeader.remove(id);
												AdapterSMSHistory
														.update(MessengerListHeader);
											}
										});
								aldialog.setNegativeButton("No",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.cancel();
											}
										});
								// AlertDialog alert = aldialog.create();
								// alert.show();
								aldialog.show();

							}
						});
						return true;
					}
				});

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:

			Intent inDasboard = new Intent(ActivityCallHistory.this,
					ActivityDashboard.class);
			startActivity(inDasboard);
			finish();

			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	/**
	 * show dialog long click item messenger
	 * 
	 * @param dialog
	 */
	Button btSendSMS, btoutgoingcall, btDelete;

	private void fMessengerShowOption(Dialog dialog) {
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.item_call_history_messenger_option);
		dialog.setCanceledOnTouchOutside(true);
		WindowManager.LayoutParams params = new WindowManager.LayoutParams(
		// WindowManager.LayoutParams.WRAP_CONTENT,
		// WindowManager.LayoutParams.WRAP_CONTENT
		);
		params.gravity = Gravity.TOP | Gravity.LEFT;
		dialog.getCurrentFocus();
		btoutgoingcall = (Button) dialog
				.findViewById(R.id.btMessengerOutgoingcall);

		btSendSMS = (Button) dialog.findViewById(R.id.btMessengerSendSMS);
		btDelete = (Button) dialog.findViewById(R.id.btMessengerDelete);
	}

	/**
	 * recall phone number
	 * 
	 * @param id
	 */
	private void fRecall(int id, Context context) {
		Intent callIntent = new Intent(Intent.ACTION_CALL);
		callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		callIntent.setData(Uri.parse("tel:"
				+ MessengerListHeader.get(id).getPhoneNumber()));
		context.startActivity(callIntent);
	}

	/**
	 * 
	 * new inten infomation history
	 * 
	 */
	private void fInfoHistory(int position) {
		Intent intent = new Intent(ActivityCallHistory.this,
				ActivityCallHistoryInformation.class);
		intent.putExtra("phonenumber", CallListHeader.get(position)
				.getPhoneNumber());
		// get phonenumber is positon -1 because sectionlist add + 1 for title
		// date (today,yesterday,..)
		String date = CallListHeader.get(position).getDateTime();
		intent.putExtra("datetime", date);
		startActivity(intent);
		finish();
	}

	/**
	 * get list messenger
	 */
	private void fMessengerHistory() {
		Drawable dCallHistory = getResources().getDrawable(R.drawable.calllist);
		Drawable dMessengerHistory = getResources().getDrawable(
				R.drawable.messaginglist_press);
		btCallHistory.setBackgroundResource(R.drawable.frame);
		btCallHistory.setCompoundDrawablesWithIntrinsicBounds(null,
				dCallHistory, null, null);
		btCallHistory.setTextColor(Color.WHITE);
		btMessengerHistory.setBackgroundResource(R.drawable.frame_press);
		btMessengerHistory.setCompoundDrawablesWithIntrinsicBounds(null,
				dMessengerHistory, null, null);
		btMessengerHistory.setTextColor(Color.BLACK);
		vfhistory
				.setInAnimation(ActivityCallHistory.this, R.anim.in_from_right);
		vfhistory.showNext();
		btMessengerHistory.setEnabled(false);
		btCallHistory.setEnabled(true);
	}

	/**
	 * get list call
	 */
	private void fCallHistory() {
		Drawable dCallHistory = getResources().getDrawable(
				R.drawable.calllist_press);
		Drawable dMessengerHistory = getResources().getDrawable(
				R.drawable.messaginglist);
		btCallHistory.setBackgroundResource(R.drawable.frame_press);
		btCallHistory.setCompoundDrawablesWithIntrinsicBounds(null,
				dCallHistory, null, null);
		btCallHistory.setTextColor(Color.BLACK);
		btMessengerHistory.setBackgroundResource(R.drawable.frame);
		btMessengerHistory.setCompoundDrawablesWithIntrinsicBounds(null,
				dMessengerHistory, null, null);
		btMessengerHistory.setTextColor(Color.WHITE);
		vfhistory.setInAnimation(ActivityCallHistory.this, R.anim.in_from_left);
		vfhistory.showPrevious();
		btMessengerHistory.setEnabled(true);
		btCallHistory.setEnabled(false);
	}

	/**
	 * Get all call in history phone
	 * 
	 */
	private void getCallDetails(Context context) {
		DatabaseHanler db = new DatabaseHanler(context);
		if (db.getCallBlockItem(null, null, null, null, null) != null) {
			CallListHistory = (ArrayList<CallBlockedItem>) db
					.getCallBlockItem(null, null, null,
							CallBlockedItem.DATE_TIME + " DESC", null);
			// set section header
			for (CallBlockedItem item : CallListHistory) {
				try{
					if(CurrentDateCall == null || CurrentDateCall.equals(null)){
						CurrentDateCall = StringToDate(item.getDate_time()).substring(0 , 11);
						HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
						headersms.setPointSection(1);
						headersms.setDateTime(item.getDate_time());
						headersms.setPhoneNumber(item.getPhoneNumber());
						headersms.setStatus(item.getStatus());
						CallListHeader.add(headersms);
					}else{
						if (CurrentDateCall.contentEquals(StringToDate(item.getDate_time()).substring(0 , 11))) {
						} else {
							CurrentDateCall = StringToDate(item.getDate_time()).substring(0 , 11);
							HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
							headersms.setPointSection(1);
							headersms.setDateTime(item.getDate_time());
							headersms.setPhoneNumber(item.getPhoneNumber());
							headersms.setStatus(item.getStatus());
							CallListHeader.add(headersms);
						}
					}
					
				}catch(Exception e){
					
				}
				
				HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
				headersms.setPointSection(0);
				headersms.setDateTime(item.getDate_time());
				headersms.setPhoneNumber(item.getPhoneNumber());
				headersms.setStatus(item.getStatus());
				CallListHeader.add(headersms);
			}
		}
	}

	/**
	 * get all messenger block
	 * 
	 * @param context
	 */
	private void fgetMessengerBlock(Context context) {
		DatabaseHanler dbhSmsBlock = new DatabaseHanler(context);
		if (dbhSmsBlock.getCallBlockItem(null, null, null, null, null) != null) {
			MessengerListHistory = (ArrayList<SmsBlockedItem>) dbhSmsBlock
					.getSmsBlockItem(null, null, null, SmsBlockedItem.DATE_TIME
							+ " DESC", null);
			// set section header
			for (SmsBlockedItem item : MessengerListHistory) {
				Log.e(TAG ,StringToDate(item
							.getDate_time()).substring(0 , 11) );
				try{
					if (CurrentDateMessenger == null || CurrentDateMessenger.equals(null)){
						CurrentDateMessenger = item
								.getDate_time().substring(0 , 11);
						HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
						headersms.setPointSection(1);
						headersms.setContent(item.getContent());
						headersms.setDateTime(item.getDate_time());
						headersms.setPhoneNumber(item.getPhoneNumber());
						headersms.setStatus(item.getStatus());
						MessengerListHeader.add(headersms);
					}else{
						if (CurrentDateMessenger.contentEquals(item
								.getDate_time().substring(0 , 11))) {
						} else {
							CurrentDateMessenger = item.getDate_time().substring(0 , 11);
							HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
							headersms.setPointSection(1);
							headersms.setContent(item.getContent());
							headersms.setDateTime(item.getDate_time());
							headersms.setPhoneNumber(item.getPhoneNumber());
							headersms.setStatus(item.getStatus());
							MessengerListHeader.add(headersms);
						}
					}
					
				}catch(Exception e){
					e.printStackTrace();
				}
				
				HeaderSMSBlockedItem headersms = new HeaderSMSBlockedItem();
				headersms.setPointSection(0);
				headersms.setContent(item.getContent());
				headersms.setDateTime(item.getDate_time());
				headersms.setPhoneNumber(item.getPhoneNumber());
				headersms.setStatus(item.getStatus());
				MessengerListHeader.add(headersms);
			}
		}
		// Toast.makeText(getApplicationContext(),""+
		// MessengerListHistory.get(4).getDate_time()
		// , Toast.LENGTH_LONG).show();
	}

	/**
	 * Test insert sms block
	 * 
	 */
	private void finsertMessengerBlock(Context context) {
		DatabaseHanler dbhSmsBlock = new DatabaseHanler(context);
		for (int i = 0; i < 100; i++) {
			dbhSmsBlock.insertSmsBlockItem(new SmsBlockedItem("01636369246",
					"2014-09-30 21:20", 1, "okok"));
			dbhSmsBlock.insertSmsBlockItem(new SmsBlockedItem("01636369247",
					"2014-09-24 21:20", 2, "ok2"));
			dbhSmsBlock.insertSmsBlockItem(new SmsBlockedItem("01636369248",
					"2014-09-23 21:20", 3, "ok1"));
		}
	}

	/**
	 * Test insert Call block
	 */
	private void finsertCallBlock(Context context) {
		DatabaseHanler db = new DatabaseHanler(context);
		for (int i = 0; i < 30; i++) {

			db.insertCallBlockItem(new CallBlockedItem("01636369241",
					"2014-09-30 21:20", 16));
			db.insertCallBlockItem(new CallBlockedItem("01636369242",
					"2014-09-28 21:20", 32));
			db.insertCallBlockItem(new CallBlockedItem("01636369243",
					"2014-09-27 21:20", 48));
			db.insertCallBlockItem(new CallBlockedItem("01636369244",
					"2014-09-26 21:20", 64));
			db.insertCallBlockItem(new CallBlockedItem("01636369245",
					"2014-09-25 21:20", 80));
			db.insertCallBlockItem(new CallBlockedItem("01636369246",
					"2014-09-24 21:20", 96));
		}
	}

	/**
	 * convert String to date
	 * 
	 * @param getdatetime
	 */
	private String StringToDate(String getdatetime) {

		// String getdatetime = MessengerListHeader.get(id).getDateTime();
		Date datetime = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			datetime = format.parse(getdatetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String date = null;
		try {
			date = dateformat.format(datetime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;

	}

	/**
	 * Broadcast update view when have event
	 */
	final BroadcastReceiver broadcastUpdateForm = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundleMessageData = intent.getExtras();
			if (bundleMessageData != null) {
				try {

					int haveCallsms = bundleMessageData.getInt("receivetype");
					int smsSaveApp = bundleMessageData.getInt("saveapp");
					// date time
					Calendar cal = Calendar.getInstance();
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					String datetime = df.format(cal.getTime());
					SimpleDateFormat df1 = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm");
					String datetimeSave = df1.format(cal.getTime());
					// 1 is call and 2 is sms
					if (haveCallsms == 1) {
						updateWhenHaveNewCall(context , bundleMessageData , datetimeSave , datetime);
					}
					if (haveCallsms == 2 ){//&& smsSaveApp == 1) {
						updateWhenReceiveSms(context , bundleMessageData , datetimeSave , datetime);
					}						
											
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}
	};

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Intent inDash = new Intent(ActivityCallHistory.this,
				ActivityDashboard.class);
		startActivity(inDash);
		finish();
	}
	
	void updateWhenHaveNewCall(Context context , Bundle bundleMessageData ,String datetimeSave , String datetime){

		String phone = bundleMessageData
				.getString("phonenumber");
		int status = bundleMessageData.getInt("callmode");
		status = status & 240;
		Toast.makeText(context, phone, Toast.LENGTH_LONG)
				.show();
		Log.v("phone", "" + phone);
		DatabaseHanler db = new DatabaseHanler(
				ActivityCallHistory.this);

		// insert list
		if (CallListHeader.size() <= 0) {
			HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
			itemlist.setDateTime(datetimeSave);
			itemlist.setPhoneNumber(phone);
			itemlist.setPointSection(1);
			itemlist.setStatus(16);
			CallListHeader.add(itemlist);
			adapter = new AdapterCallHistory(CallListHeader,
					inf, context);
			listView.setAdapter(adapter);
		} else {
			// check list
			ArrayList<CallBlockedItem> item1 = new ArrayList<CallBlockedItem>();
			item1 = (ArrayList<CallBlockedItem>) db
					.getCallBlockItem(
							null,
							CallBlockedItem.DATE_TIME
									+ " LIKE ? ",
							new String[] { "%" + datetime + "%" },
							null, null);
			if (item1.size() == 1) {
				HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
				itemlist.setDateTime(datetimeSave);
				itemlist.setPhoneNumber(phone);
				itemlist.setPointSection(1);
				itemlist.setStatus(status);
				CallListHeader.add(0, itemlist);
				adapter.update(CallListHeader);
				Toast.makeText(ActivityCallHistory.this,
						"?" + datetime, Toast.LENGTH_LONG)
						.show();
			}
		}

		HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
		itemlist.setDateTime(datetimeSave);
		itemlist.setPhoneNumber(phone);
		itemlist.setPointSection(0);
		itemlist.setStatus(16);
		CallListHeader.add(itemlist);
		adapter.update(CallListHeader);

	}
	
	void updateWhenReceiveSms(Context context , Bundle bundleMessageData , String datetimeSave , String datetime ){
		DatabaseHanler db = new DatabaseHanler(
				ActivityCallHistory.this);
		String phone = bundleMessageData
				.getString("phonenumber");
		String messenger = bundleMessageData
				.getString("messenger");
		int status = bundleMessageData.getInt("modesms");
		status = status & 15;
		Log.e(TAG , " nhan dc tin nhan o call history");
//		// insert list
//		if (MessengerListHeader.size() <= 0) {
//			HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
//			itemlist.setDateTime(datetimeSave);
//			itemlist.setPhoneNumber(phone);
//			itemlist.setPointSection(1);
//			itemlist.setStatus(status);
//			itemlist.setContent(messenger);
//			MessengerListHeader.add(itemlist);
//			adapter = new AdapterCallHistory(
//					MessengerListHeader, inf, context);
//			lvMessengerHistory.setAdapter(adapter);
//		} else {
//			// check list
//			ArrayList<CallBlockedItem> item1 = new ArrayList<CallBlockedItem>();
//			item1 = (ArrayList<CallBlockedItem>) db
//					.getCallBlockItem(
//							null,
//							CallBlockedItem.DATE_TIME
//									+ " LIKE ? ",
//							new String[] { "%" + datetime + "%" },
//							null, null);
//			Log.e(TAG , "vao day roi");
//			if (item1.size() == 1) {
//				HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
//				itemlist.setDateTime(datetimeSave);
//				itemlist.setPhoneNumber(phone);
//				itemlist.setPointSection(1);
//				itemlist.setStatus(status);
//				MessengerListHeader.add(0, itemlist);
//				AdapterSMSHistory.update(MessengerListHeader);
//			}
//		}

		
		fgetMessengerBlock(getApplicationContext());
		AdapterSMSHistory = new AdapterMessengerHistory(MessengerListHeader,
				inf, getApplicationContext());
		lvMessengerHistory.setAdapter(AdapterSMSHistory);
		
//		HeaderSMSBlockedItem itemlist = new HeaderSMSBlockedItem();
//		itemlist.setDateTime(datetimeSave);
//		itemlist.setPhoneNumber(phone);
//		itemlist.setPointSection(0);
//		itemlist.setStatus(status);
//		itemlist.setContent(messenger);
//		if(MessengerListHeader == null || MessengerListHeader.equals(null) || MessengerListHeader.size() == 0){
//			MessengerListHeader = new ArrayList<HeaderSMSBlockedItem>();
//		}
//		MessengerListHeader.add(0 ,itemlist);
//		adapter.update(MessengerListHeader);
//		lvMessengerHistory.setSelection(0);
//		Toast.makeText(context, "vkl chym se", Toast.LENGTH_SHORT).show();
	}
	
}
